import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.Vector;


import com.aliasi.tokenizer.NGramTokenizerFactory;
import com.aliasi.tokenizer.Tokenizer;
import com.atlassian.jira.rest.client.domain.Status;



/*@Sangeeta
 * Program to do ngram matching
 * @NOTE: This program is based on new_fig_bugid table @@ Which need to changed in future. DONE
 * ALL other processing is done on the based on fid_bugid tables not on the n_fid_bugid. Corrected
 * 1.Title+Description and directory name
 * */

/*Change1= Change get_file_path() table if needed.
 *change 2= token matching needs to done.
 *change 3= File_path + title/desc matching needs to be done.  
 *change 4= I need to store only distinct tokens.: DONE
 * */
public class ngram_match 
{
     public String dbname="jboss";
     public String user="root";
     public String passwd="123";
	     
     
	//process
	public void process(Connection conn)
	{
		//Connection conn  =null;
		Statement stmt=null;
		
		//selecting only selected Bugs 
		String q= "select * from  n_fid_bugid";
		
		try
		  {
			 stmt=conn.createStatement();
			 stmt.execute(q);
		     ResultSet rs =  stmt.getResultSet();
		     
		     int count=1;
		     while(rs.next())
		       {
		    	 
		    	 String nfid=rs.getString("nfid");
		    	 String bugid=rs.getString("bugid");
		    	 String svnid=rs.getString("svnid");
		    	 
		    	 boolean valid = check_valid_bugid(bugid,conn);//check whether bug id closed+done or not
		    	 
		    	if(valid)
		    	 {
		    		String title=get_title(bugid,conn);
		    		String desc= get_desc(bugid,conn);
		    		String fname=get_fname(nfid,conn);
		    		String file_path=get_file_path(nfid,conn);
		    	  
		    		 fname=  fname.trim();
		    		 boolean java_file = fname.endsWith(".java"); 
		    		 
		    		if(java_file)
		    			{
		    				
		    				System.out.println("title="+title);
		    				System.out.println("fname="+fname);
	    				
		    			    general_fun gf= new general_fun();
		    	 
		    				title=gf.clean(title);
		    				desc=gf.clean(desc);
		    	 
		    				title=gf.remove_stop_words(title);
		    				desc=gf.remove_stop_words(desc);
		    	
		    				title=gf.remove_SW(title);
		    				desc=gf.remove_SW(desc);
		    	
		    				fname =  fname.replaceAll(".java", "");
		    				
		    				//FIle processing can be done here just in case just inn case If I am considering whole path
		    				file_path=gf.remove_slash(file_path);
		    				file_path=remove_dup(file_path);/*remove duplicate from the file_path as there are multiple paths
															for same file*/
		    				file_path=gf.clean(file_path);
		    				file_path=gf.remove_stop_words(file_path);
		    				file_path=gf.remove_SW(file_path);
		    				//file_path=gf.remove_partial(file_path);//NOT Needed Currently, branch,branches added to stop_words1 list
		    	 
		    				//fname=gf.remove_SW_words(fname);
		    	 
		    	 
		    				//clean_file();//may be needed in case we are working on full path of file.
		    	 
		    				//OPTIMIZE
		    				/*
		    				String[] t_ngram=ngram(title);
		    				String []d_ngram=ngram(desc);
		    				String []f_ngram=ngram(fname);*/
		    				
		    				
		    				int tf_ngram=get_f_ngram(title,fname);
		    				int df_ngram=get_f_ngram(desc,fname);
		    				int tpath_ngram=get_tpath_ngram(title,file_path);
		    				int dpath_ngram=get_dpath_ngram(desc,file_path);
		    				//@@NOT A COMMENT
		    				
		    				
		    				//System.out.println("title="+title);
		    				//System.out.println("fname="+fname);
		    				//System.out.println("tf_ngram = "+tf_ngram);
		    				//System.out.println("df_ngram ="+df_ngram);
		    				
		    	 
		    				int tf_token=0;//get_tf_token(title,fname);
		    				int df_token=0;//get_df_token(desc,fname);
		    				int tpath_token=0;//get_tpath_token(title,file_path);
		    				int dpath_token=0;//get_dpath_token(desc,file_path);
		    	 
		    				//insert
		    				
		    				insert_to_tpd_score(nfid,bugid,svnid,title,desc,fname,file_path,tf_ngram,df_ngram,tf_token,df_token,
		    						tpath_ngram,dpath_ngram,tpath_token,dpath_token,conn);
		    						
		    	            //@@NOT A COMMENT
		    				
		    				//intr();
		    				System.out.println("count="+count);
		    				count++;
		    			
		    			}//if
		    		
		    	 }//if valid	
		      
		       }//wh
			 
		  
		     if(rs!=null)
		    	 rs.close();
		     
		     if(stmt!=null)
		    	 stmt.close();
		  
		  } catch (SQLException e) 
		  { 	e.printStackTrace(); 	}
		
		
}//process


private String remove_dup(String file_path) 
{
	Vector<String> unique_path_tokens= new Vector<String>();
	file_path=file_path.replaceAll("\n", " ");
	String []file_path_arr=file_path.split("\\s+");
	
	unique_path_tokens.add(file_path_arr[0]);
	for(int i=1;i<file_path_arr.length;i++)
	{
		boolean contains=false;
       for(int j=0;j<unique_path_tokens.size();j++)
       {
    	   if(unique_path_tokens.get(j).equalsIgnoreCase(file_path_arr[i]))
    	   {
    		   contains=true;
    		   break;
    	   }
        }
       
         if(!contains)
         {
        	 unique_path_tokens.add(file_path_arr[i]);
         }
       
      }//for


	file_path="";
	for(int i=0;i<unique_path_tokens.size();i++)
	{
		file_path+=unique_path_tokens.get(i)+" ";
	}
	return file_path;

}//for


private boolean check_valid_bugid(String bugid, Connection conn) 
{
  Statement stmt=null;
  String status = "";
  String resolution="";
  boolean valid=false;
  String q= "select status, resolution from bug_svn_info where bugid='"+bugid+"'";
  
  try 
  {
	   stmt=conn.createStatement();
	   stmt.execute(q);
	   ResultSet rs =  stmt.getResultSet();
	   while(rs.next())
	   {
		   status =  rs.getString("status");
		   resolution= rs.getString("resolution");
	   }
	   
	   
	  if(rs!=null)
		  rs.close();
	   
	   if(stmt!=null)
			  stmt.close();
	   
	   if((status.equalsIgnoreCase("closed"))&&(resolution.equalsIgnoreCase("done")))
			{
		         valid=true;
			 }
	   else
	   		{
		     valid=false;
	   		}
	   
   } catch (SQLException e) 
   { e.printStackTrace(); }
  
  
   
  
return valid;	
		
}


private void insert_to_tpd_score(String nfid, String bugid, String svnid,
			String title, String desc, String fname,String file_path, int tf_ngram,
			int df_ngram, int tf_token, int df_token, int tpath_ngram,
			int dpath_ngram, int tpath_token, int dpath_token, Connection conn) 
{
		
	Statement stmt=null;
	String insert = "insert into tdfp_match_score values('"+nfid+"','"+bugid+"','"+svnid+"','"+title+"','"+
					desc+"','"+fname+"','"+file_path+"',"+tf_ngram+","+df_ngram+","+tf_token+","+df_token+","+tpath_ngram+
					","+dpath_ngram+","+tpath_token+","+dpath_token+")";
	
	try 
	{
			stmt=conn.createStatement();
			stmt.executeUpdate(insert);
	} catch (SQLException e) 
	{ 	e.printStackTrace(); }
	
}

/*
private void insert_to_tpd_score(String fid, String bugid, String svnid,String title, String desc,
		          String fname, int tp_ngram, int td_ngram,int tp_token, int td_token, Connection conn) 
{
		
	Statement stmt=null;
	String insert = "insert into tdp_match_score values('"+fid+"','"+bugid+"','"+svnid+"','"+title+"','"+
					desc+"','"+fname+"','"+tp_ngram+"','"+td_ngram+"','"+tp_token+"','"+td_token+"')";
	
	try 
	{
			stmt=conn.createStatement();
			stmt.executeUpdate(insert);
	} catch (SQLException e) 
	{ 	e.printStackTrace(); }
	
	

}//insert
*/

//Spaces are not removed form the title because, they will never match with the file name  
private int get_f_ngram(String title, String fname)
{
		
	int result = commonCharacterNGrams(title,fname);
    
	int no_of_ngrams=ngram(fname).length;
	
	//if(result!=0)
	  {
		//result=((result*100)/no_of_ngrams);
	  }	
	return result;
}
/*
private int get_df_ngram(String desc, String fname)
{
		
	int result = commonCharacterNGrams(desc,fname);
    //int no_of_ngrams=ngram(fname).length;
	
	//if(result!=0)
	  {
		//result=((result*100)/no_of_ngrams);
	  }	
	return result;
}
*/

private int get_tpath_ngram(String title, String path)
{
	String []path_arr = path.split("\\s+");
	String []title_arr = title.split("\\s+");
	
	int result = 0;
	for(int i=0;i<path_arr.length;i++)
		{
	
		   for(int j=0;j<title_arr.length;j++)
		   {
			   result+=commonCharacterNGrams(path_arr[i], title_arr[j]);
		   }
		
		
		}//for

	return result;
}

private int get_dpath_ngram(String desc, String path)
{
		
	//int result = commonCharacterNGrams(desc,file_path);
	
	String []path_arr = path.split("\\s+");
	String []desc_arr = desc.split("\\s+");
	
	int result = 0;
	for(int i=0;i<path_arr.length;i++)
		{
	
		   for(int j=0;j<desc_arr.length;j++)
		   {
			   result+=commonCharacterNGrams(path_arr[i], desc_arr[j]);
		   }
		
		
		}//for


	return result;
}

//=============
private int get_tf_token(String title, String fname)
{
		

	int result = commonCharacterNGrams(title,fname);

	return result=0;
}

private int get_df_token(String desc, String fname)
{
		
	int result = commonCharacterNGrams(desc,fname);

	return result=0;
}

private int get_tpath_token(String desc, String fname)
{
		
	int result = commonCharacterNGrams(desc,fname);
     
	
	return result=0;
}

private int get_dpath_token(String desc, String fname)
{
		
	int result = commonCharacterNGrams(desc,fname);

	return result=0;
}


public int commonCharacterNGrams(String a, String b)
{
	
	    System.out.println("---------------------------------");
		int result = 0;
		String[] aNgrams = ngram(a);

		String[] bNgrams = ngram(b);
		
		for(int  i=0;i<aNgrams.length;i++ )
		{
			System.out.println(i+"="+aNgrams[i]);
		}
		
		for(int  i=0;i<bNgrams.length;i++ )
		{
			System.out.println(i+"="+bNgrams[i]);
		}
		
		
        int count=0;
		for(int i=0;i<aNgrams.length;i++)
		{
			String x = aNgrams[i];

			for(int j=0;j<bNgrams.length;j++)
				{
					String y = bNgrams[j];

						if(x.equalsIgnoreCase(y))
						{
						    count++;
							System.out.println("count="+count+" "+x + " : " + y);
							System.out.println("len = "+x.length()+" "+y.length());
						    result = result + 1;
						}
				}
		}//for

	System.out.println("Result = "+result);	
	System.out.println("----------------------------------");	
		return result;
}


public String[] ngram(String inputText)
{
		String[] result =new String[0];
		
		NGramTokenizerFactory ngtf =new NGramTokenizerFactory(3,10);
		Tokenizer tokenizer = ngtf.tokenizer(inputText.toCharArray(), 0, inputText.length());

		Iterator<String> iterator = tokenizer.iterator();

		result = tokenizer.tokenize();

		/*
		System.out.println(result.length);

		for(int i=0;i<result.length;i++){

		System.out.println((i+1) + " : " + result[i]);

		}

		*/

		/*
		while(iterator.hasNext()){

		String token = (String)iterator.next();

		System.out.println(token);

		}

		*/

		return result;
}



public String get_file_path(String nfid, Connection conn) 
{

	  String file_path="";
	  
	  Statement stmt=null;
	  String q= "select * from  n_fname_fdir where nfid='"+nfid+"'";
		
		try
		  {
			 stmt=conn.createStatement();
			 stmt.execute(q);
		     ResultSet rs =  stmt.getResultSet();
		     while(rs.next())
		        {
		    	 
		    	  file_path=rs.getString("fdirs");
		    	  //file_path+=file_path+"/";
		    	 
		        }//wh
			 
		  
		     if(rs!=null)
		    	 rs.close();
		     
		     if(stmt!=null)
		    	 stmt.close();
		  
		  } catch (SQLException e) 
		  { 	e.printStackTrace(); 	}
	  
	  return file_path;
}//


public String get_fname(String nfid, Connection conn) 
{
  String fname="";
 
  Statement stmt=null;
	String q= "select * from  n_fname_fdir where nfid='"+nfid+"'";
	
	try
	  {
		 stmt=conn.createStatement();
		 stmt.execute(q);
	     ResultSet rs =  stmt.getResultSet();
	     while(rs.next())
	        {
	    	 
	    	  fname=rs.getString("fname");
	    	
	    	 
	        }//wh
		 
	  
	     if(rs!=null)
	    	 rs.close();
	     
	     if(stmt!=null)
	    	 stmt.close();
	  
	  } catch (SQLException e) 
	  { 	e.printStackTrace(); 	}
  
  return fname;
  
	
}


public String get_title(String bugid, Connection conn) 
{
	String title="";
	
	Statement stmt=null;
	String q= "select * from  bug_svn_info where bugid='"+bugid+"'";
	System.out.println("String="+q);
	
	try
	  {
		 stmt=conn.createStatement();
		 stmt.execute(q);
	     ResultSet rs =  stmt.getResultSet();
	     while(rs.next())
	        {
	    	 
	    	 title=rs.getString("summary");
	    		    	 
	        }//wh
		 
	  
	     if(rs!=null)
	    	 rs.close();
	     
	     if(stmt!=null)
	    	 stmt.close();
	  
	  } catch (SQLException e) 
	  { 	e.printStackTrace(); 	}
	
	
	return title;
	
}

public String get_desc(String bugid, Connection conn)
{
String desc="";

Statement stmt=null;
String q= "select * from  bug_svn_info where bugid='"+bugid+"'";

try
  {
	 stmt=conn.createStatement();
	 stmt.execute(q);
     ResultSet rs =  stmt.getResultSet();
     while(rs.next())
         {
    	 
    	  desc=rs.getString("description");
    	 
         }//wh
	 
  
     if(rs!=null)
    	 rs.close();
     
     if(stmt!=null)
    	 stmt.close();
  
  } catch (SQLException e) 
  { 	e.printStackTrace(); 	}

return desc;
	
}
	//main
public static void main(String args[])
{
	   ngram_match nm =  new ngram_match();
	   Connection conn=null;
	   db_conn db =  new db_conn();
	   conn=db.connect(nm.dbname, nm.user, nm.passwd);
	   nm.process(conn);
	   db.close(conn);
	
	   
}//main
/*
public void intr()
{
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));	
   System.out.println("INTR");
   try {
	br.readLine();
} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
}*/
	
}//
